package br.com.skpharma.dao;

import java.util.ArrayList;

import br.com.skpharma.db.DB;
import br.com.skpharma.vo.LaboratorioVO;


public class DAOLaboratorio {
	private DB db = new DB("jdbc:postgresql://localhost:5432/skpharma","postgres","postgres");
	private LaboratorioVO laboratorio;

	public DAOLaboratorio(){
		laboratorio = new LaboratorioVO();
	}
	
	public void setLaboratorio(LaboratorioVO nLaboratorioVO){
		laboratorio = nLaboratorioVO;
	}

	public LaboratorioVO getLaboratorioVO(){
		return(laboratorio);
	}

	public void salvar(){

		if(db.connect()){
		
			db.update("insert into laboratorio (ds_laboratorio) values ('"+ 
				 	laboratorio.getDs()+ "')");
			db.disconnect();
		}		
	}

	public void excluir(){
		if(db.connect()){
			db.update("delete from laboratorio where cd_laboratorio = " + laboratorio.getCod() );			
			db.disconnect();
		}		
	}
	
	public void editar(){		
		if(db.connect()){
			db.update("update laboratorio set ds_laboratorio='"+ laboratorio.getDs() +
					"'where cd_laboratorio= " + laboratorio.getCod());			
			db.disconnect();
		}		
	}

	
	
	public ArrayList obterLista(){
		ArrayList lista = new ArrayList();
		LaboratorioVO laboratorioVO;
		DAOCidade daoCidade;
		if(db.connect()){
			db.select("select laboratorio.* from laboratorio order by ds_laboratorio");			
			while(db.moveNext()){
				laboratorioVO = new LaboratorioVO( db.getInt("cd_laboratorio"),  db.getString("ds_laboratorio"));
				lista.add(laboratorioVO);
			}
			db.disconnect();
		}		
		return lista;
	}	
	

	public void obter(int codigo){		
		if(db.connect()){
			db.select("select laboratorio.* from laboratorio where cd_laboratorio =" + codigo);
			if(db.moveNext()){				
				laboratorio.setCod(db.getInt("cd_laboratorio"));
				laboratorio.setDs(db.getString("ds_laboratorio"));				
			}
			
			db.disconnect();
		}		
	}
	
	public void carregar(int codigo){		
		if(db.connect()){
			db.select("select cd_laboratorio, ds_laboratorio from laboratorio where cd_laboratorio=" + codigo);
			if(db.moveNext()){
				laboratorio.setCod(db.getInt("cd_laboratorio"));
				laboratorio.setDs(db.getString("ds_laboratorio"));
			}
			db.disconnect();
		}		
	}
}

